package com.xxxx.server.controller;

import com.xxxx.server.pojo.*;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IRoleService;
import com.xxxx.server.service.impl.AdminUpdateService;
import com.xxxx.server.utils.SendMailUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.Principal;

/**
 * 登录
 *
 * @author zhoubin
 * @since 1.0.0
 */
@Api("登录")
@RestController
public class LoginController {

	@Autowired
	private IAdminService adminService;

	@Autowired
    private IRoleService roleService;


	@ApiOperation("登录")
	@PostMapping("/login")
	public RespBean login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request) {
		return adminService.login(adminLoginParam.getUsername(),
                adminLoginParam.getPassword(),adminLoginParam.getCode(),request);
	}


	@ApiOperation("获取当前登录用户信息")
	@GetMapping("/admin/info")
	public Admin getAdminInfo(Principal principal) {
		if (null == principal) {
			return null;
		}
		String username = principal.getName();
		Admin admin = adminService.getUserByUsername(username);
		admin.setPassword(null);
        admin.setRoles(roleService.getRolesByAdminId(admin.getId()));
		return admin;
	}

	@Autowired
	AdminUpdateService adminUpdateService;
	@ApiOperation("更新管理员数据")
	@PutMapping("/admin/info")
	public RespBean adminUpdateController(@RequestBody AdminUpdate adminUpdate){
		adminUpdateService.adminUpdate(adminUpdate);
		return RespBean.success("更新成功！");
	}
	@ApiOperation("更新管理员密码")
	@RequestMapping("/admin/pass")
	public RespBean adminUpdatePwdController(@RequestBody AdminUpdatePwd adminUpdatePwd){
		adminUpdateService.adminUpdatePwd(adminUpdatePwd);
		return RespBean.success("修改密码成功！");
	}

	@ApiOperation("更改头像")
	@RequestMapping("/admin/userface")
	public RespBean adminUpdateUserFaceController(@RequestParam(value = "id") int id, MultipartFile file) throws IOException {
		Admin admin=new Admin();
		admin.setId(id);
		String s = adminUpdateService.adminUpdateUserFace(admin, file);
		RespBean respBean=new RespBean();
		respBean.setMessage("图片修改成功!");
		respBean.setObj(s);
		return respBean;
	}


	@ApiOperation("退出登录")
	@PostMapping("/logout")
	public RespBean logout() {
		return RespBean.success("注销成功！");
	}
}
